package com.karry.yl.web.personnel;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.LayTableResponse;
import com.karry.yl.domain.personnel.Post;
import com.karry.yl.service.personnel.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/post")
public class PostController extends SuperController {

    @Autowired
    private PostService postService;



    @RequestMapping
    public String listUI(){
        return "personnel/post/list";
    }

    @RequestMapping("/add")
    public String addUI(){
        return "personnel/post/add";
    }

    @RequestMapping("edit")
    public String editUI(Long id, ModelMap map){
        Post post = postService.selectById(id);
        map.put("post",post);
        return "personnel/post/edit";
    }

    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse listUI(int page, int limit){
        Page<Post> listPage = postService.selectPage(new Page<Post>(page, limit),new EntityWrapper<Post>().eq("orgId",currentUser.getOrgId()).eq("deleted",false).orderBy("createdTime",false));
        return new LayTableResponse(listPage.getRecords(),listPage.getTotal());
    }

    @RequestMapping("/save")
    public String save(Post post){
        post.setOrgId(currentUser.getOrgId());
        postService.insert(post);
        return "redirect:/post";
    }

    @RequestMapping("/update")
    public String update(Post post){
        post.setOrgId(currentUser.getOrgId());
        postService.updateById(post);
        return "redirect:/post";
    }

    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") List<Long> ids){
        postService.deleteBatchIds(ids);
        return "redirect:/post";
    }
}
